package com.lottery.im.module.result.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.AllGameInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.module.result.adapter.AllGameAdapter;
import com.lottery.im.module.result.presenter.AllGamePresenter;
import com.lottery.im.module.result.ui.game.Game28Fragment;
import com.lottery.im.module.result.ui.game.GameFastFragment;
import com.lottery.im.module.result.ui.game.GameLotteryFragment;
import com.lottery.im.module.result.ui.game.GamePkFragment;
import com.lottery.im.module.result.view.AllGameView;
import java.util.ArrayList;
import java.util.List;

/**
 * 所有彩种
 */
public class AllGameFragment extends BaseSwipeFragment<AllGamePresenter> implements AllGameView {

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.game_recycler) RecyclerView mRecyclerView;
  private List<AllGameInfo> mAllGameList = new ArrayList<>();
  private AllGameAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_all_game;
  }

  @Override protected void initPresenter() {
    presenter = new AllGamePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    mTvTitle.setText(getString(R.string.all_game));
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mAdapter = new AllGameAdapter(mAllGameList);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnGameClickListener((position, childPosition) -> {
      String gameType = mAllGameList.get(position)
          .getChildGameTypeList()
          .get(childPosition)
          .getGameType();
      if (Constants.lotteryArr.contains(gameType) || Constants.sd115Arr.contains(gameType)) {
        startFragment(GameLotteryFragment.create(gameType,
            mAllGameList.get(position)
                .getChildGameTypeList()
                .get(childPosition)
                .getGameTypeName()));
      } else if (Constants.pkArr.contains(gameType)) {
        startFragment(GamePkFragment.create(gameType,
            mAllGameList.get(position)
                .getChildGameTypeList()
                .get(childPosition)
                .getGameTypeName()));
      } else if (Constants.fastArr.contains(gameType)) {
        startFragment(GameFastFragment.create(gameType,
            mAllGameList.get(position)
                .getChildGameTypeList()
                .get(childPosition)
                .getGameTypeName()));
      } else if (Constants.bj28Arr.contains(gameType)) {
        startFragment(Game28Fragment.create(gameType,
            mAllGameList.get(position)
                .getChildGameTypeList()
                .get(childPosition)
                .getGameTypeName()));
      }
    });
    presenter.queryAllGame();
  }

  @OnClick({ R.id.iv_back })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
    }
  }

  @Override public void querySuccess(List<AllGameInfo> list) {
    mAllGameList.clear();
    mAllGameList.addAll(list);
    mAdapter.notifyDataSetChanged();
  }
}
